const multer = require('multer')
const express = require('express')
const Snowflake = require('../Snowflake')

module.exports = function(app,entitys,loginRouter,db){
  //文件存放路径
  let filepath = './uploads'
  // 静态文件访问路由
  app.use('/file',express.static(filepath))
  //文件上传
  app.post('/upload', loginRouter, multer({
    preservePath: true,
    storage: multer.diskStorage({
      destination: filepath,
      filename: function (req, file, next) {
        let name = Snowflake() + '_' + file.originalname
        next(null, name)
      }
    })
  }).single('file'), function (req, res) {
    req.file.path = '/file/' + req.file.filename
    res.send(req.file)
  })
}